package com.example.myapplication.tack_30.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.tack_33.adapter.CaiDanAdapter;
import com.example.myapplication.tack_33.data.CaiDanData;
import com.example.myapplication.utils.OkHttpTool;
import com.example.myapplication.utils.StateData;
import com.google.android.material.tabs.TabLayout;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class OrderFragment extends Fragment {
    private View view;
    private TabLayout fenLei;
    private RecyclerView caiDan;
    private List<String> listTab;
    private String url;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.tack_30_fragment_order, container, false);
        init();
        initTab();
        url = "prod-api/api/takeout/order/list";
        getCaiDan(url);
        return view;
    }

    private void getCaiDan(String url) {
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, CaiDanData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                CaiDanData caiDanData = (CaiDanData)obj;
                if(caiDanData.getCode() == 200){
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            caiDan.setLayoutManager(new LinearLayoutManager(getActivity()));
                            caiDan.setAdapter(new CaiDanAdapter(caiDanData.getRows(), getActivity(), new CaiDanAdapter.NetClick() {
                                @Override
                                public void click(String order) {
                                    refund(order);
                                }
                            }));
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void refund(String order) {
        String url = "prod-api/api/takeout/order/refound";
        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject1.put("orderNo",order);
            jsonObject1.put("reason","不好意思，不想要了");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String json = jsonObject1.toString();
        OkHttpTool.doNetRequest(url, OkHttpTool.POST, StateData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                StateData stateData = (StateData)obj;
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        if(stateData.getCode() == 200){
                            Toast.makeText(getActivity(),"退款成功",Toast.LENGTH_SHORT).show();
                            getCaiDan(url);
                        }
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        },json);
    }

    private void initTab() {
        for (String s : listTab) {
            TabLayout.Tab tab = fenLei.newTab();
            tab.setText(s);
            fenLei.addTab(tab);
        }
        clickTab();
    }

    private void clickTab() {
        fenLei.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                if("全部".equals(listTab.get(position))){
                    url = "prod-api/api/takeout/order/list";
                    getCaiDan(url);
                }else {

                    getCaiDan(url + "?status="+listTab.get(position));
                }
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void init(){
        fenLei =view.findViewById(R.id.fenLei);
        caiDan =view.findViewById(R.id.caiDan);
        listTab = new ArrayList<>();
        listTab.add("全部");
        listTab.add("待支付");
        listTab.add("待评价");
        listTab.add("退款");
    }
}